package com.yyw.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yyw.bean.Employee;
import com.yyw.common.PageParam;
import com.yyw.common.R;
import com.yyw.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee , HttpSession session) {
        //1、调用service
        Employee loginEmployee = employeeService.login(employee);

        //2、返回结果
        if (loginEmployee != null){
            //2-1登录成功
            //把登录查询出来的员工对象保存到session作用域，后续会话可以使用
            session.setAttribute("employee", loginEmployee);

            //给客户端响应，把登录查询到的对象返回给前端
            return R.success(loginEmployee);
        }
        //登录失败
        return R.error("用户名或密码错误！");
    }


    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        //让session失效
        session.invalidate();

        return R.success("退出成功！");
    }


    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee ){
        //0. 补充数据 通过公共字段填充
        //设置初始密码是身份证后六位
        //System.out.println(employee.getIdNumber().substring(12));
        String password = DigestUtils.md5DigestAsHex(employee.getIdNumber().substring(12).getBytes());
        employee.setPassword(password);

/*        //设置创建时间和更新时间都是现在
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //设置是哪个用户完成的这个创建和更新的工作。谁登录就是谁负责的。
        Employee nowLoginUser = (Employee) session.getAttribute("employee");
        employee.setCreateUser(nowLoginUser.getId());
        employee.setUpdateUser(nowLoginUser.getId());*/

        //1. 调用service
        int row = employeeService.add(employee);

        //2. 判定结果
        if (row > 0){
            return R.success("添加成功！");
        }

        return R.error("添加失败！");
    }


    /**
     * 员工分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam) {
        //调用service
        IPage<Employee> page = employeeService.findPage(pageParam);

        System.out.println("page = " + page);

        //返回结果
        return R.success(page);
    }


    /**
     * 更新员工
     * @param employee
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee){
        //补充数据 通过公共字段填充
/*        Employee e = (Employee) session.getAttribute("employee");
        employee.setUpdateUser(e.getId());
        employee.setUpdateTime(LocalDateTime.now());*/

        //调用service
        int row = employeeService.update(employee);

        //判断返回更新结果
        if (row > 0){
            return R.success("更新成功！");
        }
        return R.error("更新失败！");
    }

}
